const ExtractTextPlugin = require('extract-text-webpack-plugin');
const webpack = require('webpack');
const path = require('path');
const HtmlwebpackPlugin = require('html-webpack-plugin');

const ENV = process.env.NODE_ENV || 'development';
module.exports = {
  entry: {
    index: [
      'react-hot-loader/patch',
      './src/index.js',
    ],
    vendor: ['dva'],
  },
  output: {
    path: path.resolve(__dirname, 'dist'),  // 指定编译后的代码位置为 dist/bundle.js
    filename: '[name].[hash].js',
  },
  resolve: {
    alias: {
      '~': path.resolve(__dirname, 'src'),
      __DEV__: ENV !== 'production',
    },
    extensions: ['.js', '.jsx'],
  },

  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
      },
      {
        test: /\.jsx$/,
        loader: 'babel-loader',
      },
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader?modules&localIdentName=[name][hash:base64:5]'],
        }),
      },
      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader?modules&localIdentName=[name][hash:base64:8]',
            'postcss-loader',
            {
              loader: 'sass-loader',
              options: {
                data: '@import "variables.scss";',
                includePaths: [
                  path.resolve(__dirname, 'src'),
                ],
              },
            },
          ],
        }),
      },
    ],
  },
  plugins: [
    new HtmlwebpackPlugin({
      title: 'dva test',
      hash: ENV === 'production',
      template: './src/templates/my-index.ejs',
    }),
    new ExtractTextPlugin({
      filename: 'style.[contenthash].css',
      allChunks: true,
      disable: ENV !== 'production',
    }),
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false,
      },
    }),
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor', // Specify the common bundle's name.
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true,
    }),
    new webpack.HotModuleReplacementPlugin(),
    // new webpack.EnvironmentPlugin({
    //   NODE_ENV: 'production', // use 'development' unless process.env.NODE_ENV is defined
    //   DEBUG: false
    // }),
  ],
  devServer: {
    contentBase: path.join(__dirname, 'dist'),
    compress: true,
    port: 8989,
    setup(app) {
      app.post('/some/path', (req, res) => {
        res.json({ add: 2 });
      });
      app.get('/some/path1', (req, res) => {
        res.json({ custom: 'response1' });
      });
    },
  },
};
